
#include "SQLIntegerExpr.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../../../utils/doubleEqual.h"
#include "../SQLDataTypeImpl.h"

string_ptr SQLIntegerExpr::class_ = make_string_ptr("SQLIntegerExpr");
SQLDataType_ptr SQLIntegerExpr::DATA_TYPE = SQLDataType_ptr(new SQLDataTypeImpl(make_string_ptr("bigint")));

SQLIntegerExpr::SQLIntegerExpr(double theNumber)
{
  number = theNumber;
}

SQLIntegerExpr::SQLIntegerExpr(double number, SQLObject_ptr parent)
{
  this->number = number;
  this->parent = parent;
}

SQLIntegerExpr::SQLIntegerExpr()
{
}

SQLIntegerExpr::~SQLIntegerExpr()
{
  // if (number != NULL)
  // {
  // 	delete number;
  // 	number = NULL;
  // }
}
double SQLIntegerExpr::getNumber()
{
  return number;
}

int SQLIntegerExpr::intValue()
{
  int a = int(number);
  return a;
}
void SQLIntegerExpr::setNumber(double theNumber)
{
  number = theNumber;
}

void SQLIntegerExpr::output(std::stringstream * buf)
{
  int a = int(number);
  // string_ptr  s = std::to_string(a);
  string_ptr s = make_string_ptr(std::to_string(a));
  // buf.append(this.number);
  (*buf)<<*s;
}

void SQLIntegerExpr::accept0(SQLASTVisitor_ptr visitor)
{
  visitor->visit(SharedObject(SQLIntegerExpr));

  visitor->endVisit(SharedObject(SQLIntegerExpr));
}

int SQLIntegerExpr::hashCode()
{
  int prime = 31;
  int result = 1;
  // result = prime * result + ((number == NULL) ? 0 : number.hashCode());
  result = prime * result + int(number);
  return result;
}

bool SQLIntegerExpr::equals(Object_ptr obj)
{
  if (this == obj.get())
  {
    return true;
  }
  if (obj == NULL)
  {
    return false;
  }
  if (getClass() != obj->getClass())
  {
    return false;
  }
  SQLIntegerExpr_ptr other = std::dynamic_pointer_cast<SQLIntegerExpr>(obj);
  // if (number == NULL)
  // {
  //   if (other->number != NULL)
  //   {
  //     return BOOL::FALSE;
  //   }
  // }
  // else
  if (!(doubleEqual::AreEqual(number, other->number)))
  {
    return false;
  }
  return true;
}

// SQLIntegerExpr *SQLIntegerExpr::clone()
SQLObject_ptr SQLIntegerExpr::clone()
{
  // return new SQLIntegerExpr(this->number);
  SQLObject_ptr clone_tmp = SQLIntegerExpr_ptr(new SQLIntegerExpr(this->number));
  SQLIntegerExpr_ptr clone_res = std::dynamic_pointer_cast<SQLIntegerExpr>(clone_tmp);

  return clone_res;
}
